package pustakahidayah.online.service.impl;

import java.util.Date;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pustakahidayah.online.entity.Buku;
import pustakahidayah.online.entity.Customer;
import pustakahidayah.online.entity.PembayaranDetail;
import pustakahidayah.online.entity.PembayaranHeader;
import pustakahidayah.online.entity.TransaksiDetail;
import pustakahidayah.online.entity.TransaksiHeader;
import pustakahidayah.online.service.AppService;

@Service("appService")
@Transactional(readOnly=true)
public class AppServiceImpl implements AppService {

    @Autowired private SessionFactory sessionFactory;

    @Override
    @Transactional(readOnly=false)
    public void saveBuku(Buku buku) {
        sessionFactory.getCurrentSession().saveOrUpdate(buku);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteBuku(Buku buku) {
        sessionFactory.getCurrentSession().delete(buku);
    }

    @Override
    @Transactional(readOnly=false)
    public List<Buku> findAllBuku() {
        return sessionFactory.getCurrentSession().createQuery("FROM Buku").list();
    }

    @Override
    @Transactional(readOnly=false)
    public Buku findAllBukuByJudul(String Judul) {
        if (Judul==null) return null;
        return (Buku) sessionFactory.getCurrentSession().createQuery("FROM Buku b WHERE b.Judul = :judul")
                .setParameter("judul", Judul)
                .uniqueResult();
    }

    @Override
    @Transactional(readOnly=false)
    public Buku findAllBukuByPengarang(String Pengarang) {
        if (Pengarang==null) return null;
        return (Buku) sessionFactory.getCurrentSession().createQuery("FROM Buku b WHERE b.Pengarang = :pengarang")
                .setParameter("pengarang", Pengarang)
                .uniqueResult();
    }

    @Override
    @Transactional(readOnly=false)
    public Buku findAllBukuByPenerbit(String Penerbit) {
        if (Penerbit==null) return null;
        return (Buku) sessionFactory.getCurrentSession().createQuery("FROM Buku b WHERE b.Penerbit = :penerbit")
                .setParameter("penerbit", Penerbit)
                .uniqueResult();
    }

    @Override
    @Transactional(readOnly=false)
    public void saveTransaksiDetail(TransaksiDetail transaksidetail) {
        sessionFactory.getCurrentSession().saveOrUpdate(transaksidetail);
    }

    @Override
    @Transactional(readOnly=false)
    public TransaksiHeader findAllTransaksiByIdBuku(String IdBuku) {
        if (IdBuku==null) return null;
        return (TransaksiHeader) sessionFactory.getCurrentSession().createQuery("FROM TransaksiHeader th.IdBuku = :idbuku")
                .setParameter("idbuku", IdBuku)
                .uniqueResult();
    }

    @Override
    @Transactional(readOnly=false)
    public PembayaranHeader findAllPembayaranById(String Id) {
        if (Id==null) return null;
        return (PembayaranHeader) sessionFactory.getCurrentSession().createQuery("FROM PembayaranHeader ph.Id = :id")
                .setParameter("id", Id)
                .uniqueResult();
    }

    @Override
    @Transactional(readOnly=false)
    public PembayaranHeader findAllPembayaranByTanggal(Date date) {
        if (date==null) return null;
        return (PembayaranHeader) sessionFactory.getCurrentSession().createQuery("FROM PembayaranHeader ph.date = :date")
                .setParameter("date", date)
                .uniqueResult();
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteTransaksiDetail(TransaksiDetail transaksidetail) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    @Transactional(readOnly=false)
    public void savePembayaranDetail(PembayaranDetail pembayarandetail) {
        sessionFactory.getCurrentSession().saveOrUpdate(pembayarandetail);
    }

    @Override
    @Transactional(readOnly=false)
    public void deletePembayaranDetail(PembayaranDetail pembayarandetail) {
        sessionFactory.getCurrentSession().delete(pembayarandetail);
    }

    @Override
    @Transactional(readOnly=false)
    public List<TransaksiHeader> findAllTransaksiDetail() {
        return sessionFactory.getCurrentSession().createQuery("FROM TransaksiHeader").list();
    }

    @Override
    @Transactional(readOnly=false)
    public void saveCustomer(Customer customer) {
        sessionFactory.getCurrentSession().saveOrUpdate(customer);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteCustomer(Customer customer) {
        sessionFactory.getCurrentSession().saveOrUpdate(customer);
    }

    @Override
    @Transactional(readOnly=false)
    public List<Customer> findAllCustomer() {
        return sessionFactory.getCurrentSession().createQuery("FROM Customer").list();
    }

    @Override
    @Transactional(readOnly=false)
    public Customer findAllCustomerByPekerjaan(String Pekerjaan) {
        if (Pekerjaan==null) return null;
        return (Customer) sessionFactory.getCurrentSession().createQuery("FROM Customer c.Pekerjaan = :pekerjaan")
                .setParameter("pekerjaan", Pekerjaan)
                .uniqueResult();
    }
}
        